﻿using System;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace BenchMark
{
  public class Entry
  {
    [DllImport("libfunc.so", CallingConvention = CallingConvention.Cdecl)]
    public static extern void sharp2native(
      [MarshalAs(UnmanagedType.LPArray)]byte[] bytes, 
      int size);

    const int C_S2N_ByteSize = 16384;
    const int C_S2N_LoopTimes = 100000;

    public static void Sharp2Native()
    {
      Random rand = new Random();
      byte[] bytes = new byte[C_S2N_ByteSize];
      rand.NextBytes(bytes);

      long once_ticks, once_ms;
      CalcRunningTime(delegate() { sharp2native(bytes, bytes.Length); }, out once_ticks, out once_ms);

      long total_ticks, total_ms;
      CalcRunningTime(delegate() {
          for (int i = 0; i < C_S2N_LoopTimes; ++i)
          {
            // rand.NextBytes(bytes);
            sharp2native(bytes, bytes.Length);
          }
        }, out total_ticks, out total_ms);

      Console.WriteLine("once -> \n" +
                        "  {0} ticks\n" +
                        "  {1} ms\n\n" +
                        "total({2}) ->\n" +
                        "  {3} ticks\n" +
                        "  {4} ms\n\n",
                        once_ticks, once_ms, 
                        C_S2N_LoopTimes,
                        total_ticks, total_ms);
    }

    public static void CalcRunningTime(Action action, out long ticks, out long ms)
    {
      var sw = Stopwatch.StartNew();
      action();
      sw.Stop();
      ticks = sw.ElapsedTicks;
      ms = sw.ElapsedMilliseconds;
    }
  }
}
